package com.woniu.inlet.web;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.woniu.command.Command;
import com.woniu.command.customerCommand.AddCustomerCommand;
import com.woniu.command.customerCommand.DeleteCustomerCommand;
import com.woniu.command.customerCommand.UpdateCustomerCommand;
import com.woniu.inlet.vo.CustomerVo;
import com.woniu.outlet.po.CustomerPo;
import com.woniu.service.CustomerService;
import com.woniu.utils.ResponseResult;
import com.woniu.utils.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@Api(tags = "客户管理")
@Slf4j
@RequestMapping("/customer/")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private SystemLogController systemLogController;

    @GetMapping("/customers")
    @ApiOperation("查询所有客户")
    public ResponseResult<List<CustomerVo>> findCustomers() throws Exception{
        List<CustomerPo>  customerPos =  customerService.findCustomers();
        List<CustomerVo> customerVos = new ArrayList<>();
        //对象的深拷贝
        for (CustomerPo item : customerPos){
            CustomerVo customerVo = new CustomerVo();
            BeanUtils.copyProperties(item,customerVo);
            customerVos.add(customerVo);
        }

        systemLogController.addSystemLog("查询所有客户");
        return new ResponseResult<List<CustomerVo>>(customerVos, "OK", ResultCode.SUCCESS.getCode());
    }

    @GetMapping("/customers/id")
    @ApiOperation("根据id查询客户")
    public ResponseResult<CustomerVo> getCustomerById(Long id) throws Exception {
        CustomerPo customerPo = customerService.getCustomerById(id);

        //对象的深拷贝

        CustomerVo customerVo = new CustomerVo();
        BeanUtils.copyProperties(customerPo, customerVo);

        systemLogController.addSystemLog("根据id查询客户");
        return new ResponseResult<>(customerVo, "OK", ResultCode.SUCCESS.getCode());
    }


    @PostMapping("/add")
    @ApiOperation("添加客户")
    @ApiOperationSupport(ignoreParameters = {"id","customerCode","spare1","spare2","spare3"})
    public ResponseResult<String> addCustomer(CustomerPo customerPo){
        Command command = new AddCustomerCommand();
        command.execute(customerPo);

        systemLogController.addSystemLog("添加客户");
        return new ResponseResult<>(ResultCode.SUCCESS.getMessage(),"添加成功",ResultCode.SUCCESS.getCode());
    }

    @PostMapping("/update")
    @ApiOperation("修改客户")
    @ApiOperationSupport(ignoreParameters = {"spare1","spare2","spare3"})
    public ResponseResult<String> updateCustomer(CustomerPo customerPo){
        Command command = new UpdateCustomerCommand();
        command.execute(customerPo);

        systemLogController.addSystemLog("修改客户");
        return new ResponseResult<>(ResultCode.SUCCESS.getMessage(),"修改成功",ResultCode.SUCCESS.getCode());
    }


    @PostMapping("/delete")
    @ApiOperation("根据id删除客户")
    public ResponseResult<String> deleteCustomer(Long id){
        Command command = new DeleteCustomerCommand();
        command.execute(id);
        systemLogController.addSystemLog("根据id删除客户");
        return new ResponseResult<>(ResultCode.SUCCESS.getMessage(),"删除成功",ResultCode.SUCCESS.getCode());
    }


}
